package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.BaseAttribute;
import com.atguigu.tingshu.model.album.BaseCategory1;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;


	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	@Autowired
	private BaseAttributeMapper baseAttributeMapper;

	@Override
	public List<JSONObject> getBaseCategoryList() {
		//创建集合
		List<JSONObject> list = new ArrayList<>();
		//查看视图
		List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectList(null);
		//构建结构
		Map<Long, List<BaseCategoryView>> map = baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
		Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator = map.entrySet().iterator();
		while (iterator.hasNext()) {
			Map.Entry<Long, List<BaseCategoryView>> next = iterator.next();
			List<BaseCategoryView> value = next.getValue();
			Long category1Id = next.getKey();
			JSONObject category1 = new JSONObject();
			category1.put("categoryId", category1Id);
			category1.put("categoryName", value.get(0).getCategory1Name());
			//构建二级分类
			Map<Long, List<BaseCategoryView>> map1 = value.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator1 = map1.entrySet().iterator();
			List<JSONObject> categoryChildList = new ArrayList<>();
			while (iterator1.hasNext()) {
				Map.Entry<Long, List<BaseCategoryView>> next1 = iterator1.next();
				List<BaseCategoryView> value1 = next1.getValue();
				Long category2Id = next1.getKey();
				JSONObject category2 = new JSONObject();
				category2.put("categoryId", category2Id);
				category2.put("categoryName", value1.get(0).getCategory2Name());
				//构建三级分类
				List<JSONObject> category3ChildList = value1.stream().map(baseCategoryView -> {
					JSONObject category3 = new JSONObject();
					category3.put("categoryId", baseCategoryView.getCategory3Id());
					category3.put("categoryName", baseCategoryView.getCategory3Name());
					return category3;
				}).collect(Collectors.toList());
				//封装到二级分类
				category2.put("categoryChild", category3ChildList);
				categoryChildList.add(category2);
			}
			//封装到一级分类
			category1.put("categoryChild", categoryChildList);
			list.add(category1);
		}
		return list;

	}
}
